package popup

import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import app.AppContext
import com.hontech.kdxclient.R
import event.OTAUpdateStepChangedEvent
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import view.ProgressBarView

class OTAProgressWindow {
    companion object {
        val Instance: OTAProgressWindow by lazy { OTAProgressWindow() }
        var isShow = false
    }

    private val view = LayoutInflater.from(AppContext).inflate(R.layout.popup_ota_progress, null)
    private val window =
        PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true)
    private val progress = view.findViewById<ProgressBarView>(R.id.id_ota_progress)
    private val textViewMessage = view.findViewById<TextView>(R.id.id_ota_text_view_message)

    private fun initUi(max: Int) {
        progress.maxValue = max.toFloat()
        progress.currentValue = 0f
        textViewMessage.text = "系统正在升级 请稍后"
    }

    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun otaUpdateStepChangedEvent(env: OTAUpdateStepChangedEvent) {
        progress.currentValue = env.step.toFloat()
    }

    fun show(view: View, max: Int) {
        if (isShow) {
            return
        }
        window.showAtLocation(view, Gravity.CENTER, 0, 0)
    }

}